package com.tzy.evaluation.controller;
import com.tzy.common.constant.Result;
import com.tzy.common.constant.ResultCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.Comments;
import com.tzy.common.utils.SnowflakeIdUtils;
import com.tzy.evaluation.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author 谭同学
 * @since 2022-03-19
 */
@Slf4j
@RestController
@RequestMapping("/evaluation/comments")
public class CommentController extends BaseController {
    @Resource
    private CommentService commentService;

    @GetMapping("/get/all/first/level/reviews/evaluationId/{evaluationId}")
    public Result getAllFirstLevelReviews(@PathVariable int evaluationId, @RequestParam(defaultValue = "0") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer size) {
        List<Map<String, Object>> list = commentService.getAllFirstLevelReviews(evaluationId, pageNum, size);
        return Result.success(list);
    }

    @GetMapping("/user/comment/evaluationId/{evaluationId}")
    public Result userComment(@PathVariable int evaluationId, @RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer size) {
        List<Map<String, Object>> list = commentService.userComment(evaluationId, pageNum, size);

        return Result.success(list);
    }

    @GetMapping("/reply/user/replyId/{replyId}/evaluationId/{evaluationId}")
    public Result replyUser(@PathVariable int replyId,@PathVariable int evaluationId, @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer size) {
        List<Map<String, Object>> list = commentService.replyUser(replyId,evaluationId, pageNum, size);
        return Result.success(list);
    }

    @GetMapping("/count/id/{id}/evaluationId/{evaluationId}")
    public Result commentCount(@PathVariable Integer id,@PathVariable Integer evaluationId) {
        int commentCount = commentService.commentCount(id, evaluationId);
        return Result.success(commentCount);
    }

    @GetMapping("/counts/evaluationId/{evaluationId}")
    public Result commentCounts(@PathVariable Integer evaluationId) {
        int commentCounts = commentService.commentCounts(evaluationId);
        return Result.success(commentCounts);
    }
    @PostMapping("/save/entity")
    public Result save( HttpServletRequest request,@RequestBody Comments comments ){
        int userId = getUserId(request);
        log.info("userId="+userId+",evaId="+comments.getEvaluationId());
//        log.info("evaluationId="+evaluationId+",replyId="+replyId+",parentId"+parentId+",content="+content);
//        Comment comments = new Comment();
//        comments.setContent(content);
//        comments.setEvaluationId(evaluationId);
//        comments.setParentId(parentId);
//        comments.setReplyId(replyId);
        comments.setId(SnowflakeIdUtils.nextId());
        comments.setUserId(userId);
        boolean save = commentService.save(comments);
        if (save){
            return Result.success(true).putData("message","评论成功");
        }else {
            return Result.failure(ResultCode.PARAM_NOT_COMPLETE,false);
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result logicalDelete(@PathVariable  long id){
        Comments comments = new Comments();
        comments.setId(id);
        comments.setIdDeleted(1);
        boolean updateById = commentService.updateById(comments);
        if(updateById){
            return Result.success(true);
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK,false);
    }
}
